﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Something similar to sift. 
    /// </summary>
    public class ExperimentalKeyFeatures : BaseForeBackgroundColorFilter
    {
        protected List<Delegate> m_aDelegate = new List<Delegate>();

        /// <summary>
        /// 
        /// </summary>
        public ExperimentalKeyFeatures()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_experimentalKeyFeatures"></param>
        internal ExperimentalKeyFeatures(ExperimentalKeyFeatures _experimentalKeyFeatures)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int x = 0;
            int y = 0;
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;


            for (y= 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    Color color = _bitmap.GetPixel(x, y);
                    int nRGB = ColorFunctions.ToIntRGB(color);
                    int nResult = Multiply(nRGB, 7);
                    nResult = BitShift(nRGB, -3);
                    nResult = Divide(nRGB, 8);
                    nResult = BitShift(nRGB, 3);
                    HSV hsv = new HSV(color);
                    hsv.S /= 2.0f;
                    nResult = ColorFunctions.ToIntRGB(hsv.ToColor());

                    _bitmap.SetPixel(x, y, nRGB == nResult ? m_ColorForeground : m_ColorBackground);
                }                
            }
            return _bitmap;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nValue"></param>
        /// <param name="_nBits"></param>
        /// <returns></returns>
        private int BitShift(int _nValue, int _nBits)
        {
            if (_nBits > 0)
            {
                return _nValue << _nBits;
            }
            else
            {
                return _nValue >> _nBits;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nValue"></param>
        /// <param name="_nDivisor"></param>
        /// <returns></returns>
        private int Divide(int _nValue, int _nDivisor)
        {
            return _nValue / _nDivisor;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_nValue"></param>
        /// <param name="_nMultiplier"></param>
        /// <returns></returns>
        private int Multiply(int _nValue,int _nMultiplier)
        {
            return _nValue * _nMultiplier;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ExperimentalKeyFeatures(this);
        }
    }
}
